package com.ruoyi.youtian.controller;

import com.ruoyi.common.annotation.AutoSetDefaultParams;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.enums.DelFlagEnum;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.youtian.domain.TemplateDetail;
import com.ruoyi.youtian.service.ITemplateDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模板详情Controller
 *
 * @author ruoyi
 * @date 2022-09-07
 */
@RestController
@RequestMapping("/youtian/detail")
public class TemplateDetailController extends BaseController
{
    @Autowired
    private ITemplateDetailService templateDetailService;

    /**
     * 查询模板详情列表
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:list')")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(TemplateDetail templateDetail)
    {
        startPage();
        List<TemplateDetail> list = templateDetailService.selectTemplateDetailList(templateDetail);
        return getDataTable(list);
    }

    /**
     * 导出模板详情列表
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:export')")
    @Log(title = "模板详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateDetail templateDetail)
    {
        List<TemplateDetail> list = templateDetailService.selectTemplateDetailList(templateDetail);
        ExcelUtil<TemplateDetail> util = new ExcelUtil<TemplateDetail>(TemplateDetail.class);
        util.exportExcel(response, list, "模板详情数据");
    }

    /**
     * 获取模板详情详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:query')")
    @GetMapping(value = "/{templateDetailId}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("templateDetailId") Long templateDetailId)
    {
        return AjaxResult.success(templateDetailService.selectTemplateDetailByTemplateDetailId(templateDetailId));
    }

    /**
     * 新增模板详情
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:add')")
    @Log(title = "模板详情", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult add(@RequestBody TemplateDetail templateDetail)
    {
        return toAjax(templateDetailService.insertTemplateDetail(templateDetail));
    }

    /**
     * 修改模板详情
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:edit')")
    @Log(title = "模板详情", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    @AutoSetDefaultParams
    public AjaxResult edit(@RequestBody TemplateDetail templateDetail)
    {
        return toAjax(templateDetailService.updateTemplateDetail(templateDetail));
    }

    /**
     * 删除模板详情
     */
//    @PreAuthorize("@ss.hasPermi('system:detail:remove')")
    @Log(title = "模板详情", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateDetailIds}")
    @ResponseBody
    public AjaxResult remove(@PathVariable Long[] templateDetailIds)
    {
        return toAjax(templateDetailService.deleteTemplateDetailByTemplateDetailIds(templateDetailIds));
    }

    /**
     * 根据条件 查询templateDetail列表
     * @param templateDetail
     * @return
     */
    @RequestMapping("/listByTemplateDetail")
    @ResponseBody
    public AjaxResult listByTemplateId(@RequestBody  TemplateDetail templateDetail)
    {
        templateDetail.setDelFlag(DelFlagEnum.NORMAL.getCode());
        List<TemplateDetail> list = templateDetailService.selectTemplateDetailList(templateDetail);
        return AjaxResult.success(list);
    }
}
